#define RegOff(num) register(c##num) 

#define MaxLineBatch 15

#define PReg 0
#define LSCReg MaxLineBatch
#define LCReg 30 //2*MaxLineBatch
#define WReg 45 //3*MaxLineBatch


float2 Point[MaxLineBatch] : RegOff(PReg);
float3 LenghtSinCos[MaxLineBatch] : RegOff(LSCReg);
float4 LineColor[MaxLineBatch] : RegOff(LCReg);

//TODO: Move World to per part
float4 World[MaxLineBatch*4] : RegOff(WReg); 

float4x4 Project;
float Radius = 1.5;


struct VertexShaderInput
{
	float4 Position : POSITION0;
	float2 Offset : TEXCOORD0;
};

struct VertexShaderOutput
{
	float4 Position : POSITION0;
	float4 LineColor : COLOR0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
	VertexShaderOutput output;

	float4 newp = float4(input.Position.x,input.Position.y,0,1);

#define idx input.Offset.y
#define lenghtSinCos LenghtSinCos[idx]

	newp.xy *= Radius;
	
	newp.x += lenghtSinCos.x*input.Offset.x;
	
	float4 save = newp;

	newp.x = (save.x * lenghtSinCos.z) + (save.y * -lenghtSinCos.y);
	newp.y = (save.x * lenghtSinCos.y) + (save.y * lenghtSinCos.z);
	
	newp.xy += Point[idx].xy;

	float4 worldPosition = mul(newp, float4x4(World[(idx*4)],
											World[(idx*4)+1],
											World[(idx*4)+2],
											World[(idx*4)+3]));
	output.Position = mul(worldPosition, Project);
	output.LineColor = LineColor[idx];

#undef lenghtSinCos
#undef idx

	return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
	return input.LineColor;
}

technique Technique1
{
	pass Pass1
	{
		//FillMode = WireFrame;
		VertexShader = compile vs_2_0 VertexShaderFunction();
		PixelShader = compile ps_2_0 PixelShaderFunction();
	}
}
